<?php

namespace app\admin\controller;

use think\Controller;

class Adverts extends Controller
{
    //页面分页
    public function page($page ,$size,$success,$message){
        $return = [
            'data' => '',
            'success' => $success,
            'messages' => $message,
            'page' =>$page,
            'size' => $size
        ];
        $data2 = model('Adverts')->order('update_time desc')->paginate($size, false, [
            'page' => $page,
            'var_page' => 'page'
        ]);
        $data = $data2->toArray();

        if ($page>$data['last_page']) {
            $data = model('Adverts')->order('update_time desc')->paginate($size, false, [
                'page' => $data['last_page'],
                'var_page' => 'page'
            ]);
            $data = $data->toArray();
            $return['page'] = $data['last_page'];
        }
        $return['data'] = $data['data'];
        $return['total'] = $data['total'];

        $return['page'] = $data['current_page'];
        return $return;
    }
    //添加广告
    public function creatAdverts(){
        $page =input('post.page');
        $size = input('post.size');


            $data = [
                'title' => input('post.title'),
                'position' => input('post.position'),
                'maxLoadNum' => input('post.maxLoadNum'),
                'href' => input('post.href'),
                'photo_link' => input('post.img'),
                'status' => 1,
                'author' =>session('username'),

            ];
//            return $size;
            $resault = model('Adverts')->creatAdverts($data);
            if ($resault==1){
                $return = $this->page($page,$size,true,"创建成功");
            }else{
                $return = $this->page($page,$size,false,$resault);
            }

        return json($return);

    }
    //删除广告
    public function deleteAdverts(){
        $page =input('delete.page');
        $size = input('delete.size');

        $id = input('delete.id');
        $basesInfo = model('Adverts')->where('id',$id)->find();
        if (!$basesInfo){
            $return = $this->page($page,$size,false,"查无此广告");
        }else{
            $result = model('Adverts')->where('id',$id)->delete();
            if ($result){
                $return = $this->page($page,$size,true,"删除成功");
        }else{
                $return = $this->page($page,$size,false,"删除失败");
            }
        }

        return json($return);

    }
    //分类广告
    public function listeAdverts(){
        $size = input('get.size');
        $page = input('get.page');

        $return = $this->page($page,$size,true,"查询成功");
        return json($return);

    }
    //修改广告
    public function updateAdverts(){
        $size = input('put.size');
        $page = input('put.page');


            $data = [
                'title' => input('put.title'),
                'position' => input('put.position'),
                'maxLoadNum' => input('put.maxLoadNum'),
                'href' => input('put.href'),
                'photo_link' => input('put.img'),
                'update_time' => time()
            ];
            $id = input('put.id');
            $AdvertsInfo =model('Adverts')->where('id',$id)->find();
            if ($AdvertsInfo){
                $result = model('Adverts')->updateAdverts($id,$data);
                if ($result==1){
                    $return = $this->page($page,$size,true,"更新成功");

                }else {
                    $return = $this->page($page,$size,false,$result);

                }
               }else{
                $return = $this->page($page,$size,false,"查无此数据");
            }
        return json($return);

    }
    //发布广告
    public function publish(){
        $size = input('put.size');
        $page = input('put.page');

        $id = input('put.id');
        $AdvertsInfo =model('Adverts')->where('id',$id)->find();
        if ($AdvertsInfo){
            $result = model('Adverts')->where('id',$id)->update(['status'=>input('put.status')]);
            if ($result==1){
                $return = $this->page($page,$size,true,"发布成功");

            }else {
                $return = $this->page($page,$size,false,"发布失败");

            }
        }else{
            $return = $this->page($page,$size,false,"查无此数据");
        }

        return json($return);

    }
}
